// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import gameData from "../gameData";
import Tool from "../../framework/Tool";

const {ccclass, property} = cc._decorator;

@ccclass
export default class getSkinMediator extends cc.Component {

    @property(cc.Node)
    unlockSingle : cc.Node = null;

    @property(cc.Node)
    unlockAll : cc.Node = null;

    @property(cc.Sprite)
    showPiece : cc.Sprite = null;

    @property(sp.Skeleton)
    role : sp.Skeleton = null;

    @property(cc.Label)
    showIndex : cc.Label = null;

    @property(cc.Node)
    btnNode : cc.Node = null;

    @property(cc.SpriteFrame)
    pieceSkinList : cc.SpriteFrame[] = [];

    callback = null;

    showOrder = 0;

    onLoad(){
        if(this.showOrder >= 7){
            this.showOrder = 7;
        }
        this.role.setSkin("pfi" + this.showOrder);
        cc.tween(this.node).to(0.2,{
            scale : 1
        }).start();
    }

    setCallBack(isUnlockAll,index,callback?){
        if(index >= 7){
            index = 7;
        }
        this.showOrder = index;
        this.showPiece.spriteFrame = this.pieceSkinList[index - 1];
        this.showIndex.string = gameData.getColorfulPieceNumber(gameData.colorfulPiece[0]) + ",7";
        this.btnNode.active = isUnlockAll;
        this.unlockSingle.active = !isUnlockAll;
        this.unlockAll.active = isUnlockAll;
        if(callback){
            this.callback = callback;
        }
    }

    btnClick(event,data){
        if(parseInt(data) == 1){
            this.node.destroy();
            switch (this.showOrder) {
                case 1 :
                    Tool.loadPrefab("mainPrefab/skinView",(res)=>{
                        let view = cc.instantiate(res);
                        view.getComponent("skinViewMediator").setCallBack(1);
                        view.parent = cc.find("Canvas");
                    });
                    break;
                case 2 :
                    Tool.loadPrefab("mainPrefab/skinView",(res)=>{
                        let view = cc.instantiate(res);
                        view.getComponent("skinViewMediator").setCallBack(1);
                        view.parent = cc.find("Canvas");
                    });
                    break;
                case 3 :
                    cc.director.loadScene("main",()=>{
                        Tool.loadPrefab("mainPrefab/skinView",(res)=>{
                            let view = cc.instantiate(res);
                            view.getComponent("skinViewMediator").setCallBack(1);
                            view.parent = cc.find("Canvas");
                        });
                    });
                    break;
                case 4 :
                    cc.director.loadScene("main",()=>{
                        Tool.loadPrefab("mainPrefab/skinView",(res)=>{
                            let view = cc.instantiate(res);
                            view.getComponent("skinViewMediator").setCallBack(1);
                            view.parent = cc.find("Canvas");
                        });
                    });
                    break;
                case 5 :
                    cc.director.loadScene("main",()=>{
                        Tool.loadPrefab("mainPrefab/skinView",(res)=>{
                            let view = cc.instantiate(res);
                            view.getComponent("skinViewMediator").setCallBack(1);
                            view.parent = cc.find("Canvas");
                        });
                    });
                    break;
                case 6 :
                    cc.director.loadScene("main",()=>{
                        Tool.loadPrefab("mainPrefab/skinView",(res)=>{
                            let view = cc.instantiate(res);
                            view.getComponent("skinViewMediator").setCallBack(1);
                            view.parent = cc.find("Canvas");
                        });
                    });
                    break;
                case 7 :
                    cc.game.emit("moveToTargetPosition");
                    break;
                case 8 :
                    cc.director.loadScene("main",()=>{
                        Tool.loadPrefab("mainPrefab/skinView",(res)=>{
                            let view = cc.instantiate(res);
                            view.getComponent("skinViewMediator").setCallBack(1);
                            view.parent = cc.find("Canvas");
                        });
                    });
                    break;
            }
        }

        if(parseInt(data) == 2){

            this.node.destroy();
            if(this.callback && this.callback != null){
                this.callback();
            }
        }
    }
}
